package com.zxymq.consumer.config;

import com.zxymq.amqp.listener.DeadLetterMessageListener;
import com.zxymq.constants.MQConstants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


/**
 * RabbitMQ交换机、队列的配置类.定义交换机、key、queue并做好绑定
 * 同时定义每个队列的ttl，队列最大长度，Qos等等
 * 这里只绑定了死信队列。建议每个队列定义自己的QueueConfig
 *
 * @author:
 * @date :2018/1/11
 */
@Configuration
@ComponentScan
public class DlxQueueConsumerConfig {
    /**
     * 死信队列的监听
     *
     * @param connectionFactory         RabbitMQ连接工厂
     * @param DeadLetterMessageListener 死信的监听者
     * @return 监听容器对象
     */
    @Bean
    public SimpleMessageListenerContainer deadLetterListenerContainer(ConnectionFactory connectionFactory,
                                                                      DeadLetterMessageListener deadLetterMessageListener) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        //container.setQueues(dlxQueue());
        container.setQueueNames(MQConstants.DLX_QUEUE);
        container.setExposeListenerChannel(true);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setMessageListener(deadLetterMessageListener);
        /** 设置消费者能处理消息的最大个数 */
        container.setPrefetchCount(100);
        return container;
    }
}
